package com.xuelang.pipeline.artifact.app.nexus3v1.fallback;

import com.xuelang.pipeline.artifact.app.nexus3v1.WebhookRepositoryCapabilityService;
import com.xuelang.pipeline.artifact.app.nexus3v1.request.CreateWebhookRepositoryRequest;
import com.xuelang.pipeline.artifact.app.nexus3v1.request.RemoveWebhookRepositoryRequest;
import com.xuelang.pipeline.artifact.app.nexus3v1.response.CreateWebhookRepositoryResponse;
import com.xuelang.pipeline.artifact.app.nexus3v1.response.RemoveWebhookRepositoryResponse;
import com.xuelang.pipeline.artifact.client.common.constant.ArtifactConstant;
import org.springframework.cloud.openfeign.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author xiwen.zxg
 * @since 2024/7/10
 */
@Slf4j
@Service
public class WebhookRepositoryServiceFallbackFactory implements FallbackFactory<WebhookRepositoryCapabilityService> {
    @Override
    public WebhookRepositoryCapabilityService create(Throwable throwable) {
        log.error("{} happened to exception with {}", this.getClass().getName(), throwable.getMessage());
        return new WebhookRepositoryCapabilityService() {

            @Override
            public CreateWebhookRepositoryResponse createCapability(CreateWebhookRepositoryRequest createWebhookRepositoryRequest) {
                CreateWebhookRepositoryResponse createWebhookRepositoryResponse = new CreateWebhookRepositoryResponse();
                createWebhookRepositoryResponse.setTid(ArtifactConstant.DEFAULT_ERROR_CODE);
                createWebhookRepositoryResponse.setAction(ArtifactConstant.DEFAULT_ERROR_MSG);
                createWebhookRepositoryResponse.setMethod(ArtifactConstant.DEFAULT_ERROR_MSG);
                createWebhookRepositoryResponse.setType(ArtifactConstant.DEFAULT_ERROR_MSG);
                createWebhookRepositoryResponse.setResult(CreateWebhookRepositoryResponse.ResultXO.builder().success(false).data(null).build());
                return createWebhookRepositoryResponse;
            }

            @Override
            public RemoveWebhookRepositoryResponse removeCapability(RemoveWebhookRepositoryRequest removeWebhookRepositoryRequest) {
                RemoveWebhookRepositoryResponse removeWebhookRepositoryResponse = new RemoveWebhookRepositoryResponse();
                removeWebhookRepositoryResponse.setTid(ArtifactConstant.DEFAULT_ERROR_CODE);
                removeWebhookRepositoryResponse.setAction(ArtifactConstant.DEFAULT_ERROR_MSG);
                removeWebhookRepositoryResponse.setMethod(ArtifactConstant.DEFAULT_ERROR_MSG);
                removeWebhookRepositoryResponse.setType(ArtifactConstant.DEFAULT_ERROR_MSG);
                removeWebhookRepositoryResponse.setResult(RemoveWebhookRepositoryResponse.ResultXO.builder().success(false).data(null).build());
                return removeWebhookRepositoryResponse;
            }
        };
    }
}
